﻿using StandardManager.DB;
using StandardManager.DBItems;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.ListBox;

namespace StandardManager.Controls
{
    public partial class PageIndexControl : UserControl
    {
        public PageIndexControl()
        {
            InitializeComponent();
        }

        public object SelectedItem { get { return this.listBox1.SelectedItem; } }
        public ObjectCollection Items { get { return this.listBox1.Items; } }

        public int SelectedIndex { get { return this.listBox1.SelectedIndex; } internal set { this.listBox1.SelectedIndex = value; } }

        public event EventHandler OnShowBookContentCmd;

        private void 隐藏ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.listBox1.SelectedItem == null) return;
            BookContents bc = this.listBox1.SelectedItem as BookContents;

            for (var i = this.listBox1.Items.Count - 1; i >= 0; i--)
            {
                BookContents bc2 = this.listBox1.Items[i] as BookContents;
                if (bc2.BookCode == bc.BookCode)
                {
                    this.listBox1.Items.RemoveAt(i);

                    this.listBox2.Items.Add(bc2);
                }
            }
        }

        private void 显示ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (this.listBox2.SelectedItem == null) return;
            BookContents bc = this.listBox2.SelectedItem as BookContents;

            for (var i = this.listBox2.Items.Count - 1; i >= 0; i--)
            {
                BookContents bc2 = this.listBox2.Items[i] as BookContents;
                if (bc2.BookCode == bc.BookCode)
                {
                    this.listBox2.Items.RemoveAt(i);

                    this.listBox1.Items.Add(bc2);
                }
            }
        }

        private bool m_showUpdateMsg = true;
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (m_showUpdateMsg == false) return;

            if (this.listBox1.SelectedItem == null) return;
            BookContents bookContents = this.listBox1.SelectedItem as BookContents;

            var selIndex = bookContents.PageIndex;
            ConnectionDatabase_S2 connectionDatabase_S2 = ConnectionDatabase_S2.BuildSqliteConnection(GlobalConfig.Instance.DBFileName);
            var booksSql = BookContents.GetRecord(bookContents.BookCode, selIndex);
            var dt = connectionDatabase_S2.SelectData(booksSql, new string[] { "BookImg" });
            for (var i = 0; i < dt.Rows.Count; i++)
            {
                var book = BookContents.FromDataRow(dt.Rows[i]);
                if (OnShowBookContentCmd != null)
                {
                    OnShowBookContentCmd(book, e);
                }

            }
            connectionDatabase_S2.Dispose();

            this.textBox1.Text = "";
            for (var i = 0; i < BooksList.Instances.Items.Count; i++)
            {
                if (BooksList.Instances.Items[i].Code == bookContents.BookCode)
                {
                    this.textBox1.Text = BooksList.Instances.Items[i].BookName + "  " + BooksList.Instances.Items[i].Code;
                    break;
                }
            }
        }

        #region 标记
        private void 标记为附录ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.listBox1.SelectedItem == null) return;
            BookContents bookContents = this.listBox1.SelectedItem as BookContents;

            bookContents.ContentChapter = "附录";
            ConnectionDatabase_S2 connectionDatabase_S2 = ConnectionDatabase_S2.BuildSqliteConnection(GlobalConfig.Instance.DBFileName);
            var booksSql = bookContents.BuildUpdateMetadataSql();
            connectionDatabase_S2.ExecuteSQL(booksSql);
            connectionDatabase_S2.Dispose();

            var t = this.listBox1.Items.IndexOf(this.listBox1.SelectedItem);
            listBox1.Items.RemoveAt(t);
            listBox1.Items.Insert(t, bookContents);

            m_showUpdateMsg = false;
            this.listBox1.SelectedItem = this.listBox1.Items[t];
            m_showUpdateMsg = true;
        }

        private void 清除标记ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.listBox1.SelectedItem == null) return;
            BookContents bookContents = this.listBox1.SelectedItem as BookContents;

            bookContents.ContentChapter = "";
            ConnectionDatabase_S2 connectionDatabase_S2 = ConnectionDatabase_S2.BuildSqliteConnection(GlobalConfig.Instance.DBFileName);
            var booksSql = bookContents.BuildUpdateMetadataSql();
            connectionDatabase_S2.ExecuteSQL(booksSql);
            connectionDatabase_S2.Dispose();

            var t = this.listBox1.Items.IndexOf(this.listBox1.SelectedItem);
            listBox1.Items.RemoveAt(t);
            listBox1.Items.Insert(t, bookContents);

            m_showUpdateMsg = false;
            this.listBox1.SelectedItem = this.listBox1.Items[t];
            m_showUpdateMsg = true;
        }
        #endregion

        #region 隐藏附录
        private void checkBoxHideFulu_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBoxHideFulu.Checked)
            {
                for (var i = this.listBox1.Items.Count - 1; i >= 0; i--)
                {
                    BookContents bc2 = this.listBox1.Items[i] as BookContents;
                    if (string.IsNullOrEmpty(bc2.ContentChapter)==false)
                    {
                        this.listBox1.Items.RemoveAt(i);

                        this.listBox2.Items.Add(bc2);
                    }
                }
            }
            else
            {
                for (var i = this.listBox2.Items.Count - 1; i >= 0; i--)
                {
                    BookContents bc2 = this.listBox2.Items[i] as BookContents;
                    if (string.IsNullOrEmpty(bc2.ContentChapter) == false)
                    {
                        this.listBox2.Items.RemoveAt(i);

                        this.listBox1.Items.Add(bc2);
                    }
                }
            }
        } 
        #endregion
    }
}
